<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>2-SQL语言中的函数 | Ywrby个人博客网站</title><meta name="keywords" content="MySQL"><meta name="author" content="Ywrby"><meta name="copyright" content="Ywrby"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="SQL语言–函数概念：将一组逻辑语句封装在方法体内，对外暴露方法名 优点：隐藏了实现细节，提高了代码的重用性 调用语法：SELECT 函数名(实参列表) 【FROM 表】; 特点：函数名与函数功能 分类： 单行函数，例如:CONCAT(str1,str2,…),LENGTH(str)等等 分组函数（也叫统计函数，聚合函数，组函数），做统计使用   字符函数123456789101112131415">
<meta property="og:type" content="article">
<meta property="og:title" content="2-SQL语言中的函数">
<meta property="og:url" content="http://ywrby.cn/2021/03/27/3-SQL%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="Ywrby个人博客网站">
<meta property="og:description" content="SQL语言–函数概念：将一组逻辑语句封装在方法体内，对外暴露方法名 优点：隐藏了实现细节，提高了代码的重用性 调用语法：SELECT 函数名(实参列表) 【FROM 表】; 特点：函数名与函数功能 分类： 单行函数，例如:CONCAT(str1,str2,…),LENGTH(str)等等 分组函数（也叫统计函数，聚合函数，组函数），做统计使用   字符函数123456789101112131415">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://ywrby.cn/img/77593329_p0.jpg">
<meta property="article:published_time" content="2021-03-27T15:22:25.000Z">
<meta property="article:modified_time" content="2021-03-28T06:58:20.000Z">
<meta property="article:author" content="Ywrby">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ywrby.cn/img/77593329_p0.jpg"><link rel="shortcut icon" href="/img/title.png"><link rel="canonical" href="http://ywrby.cn/2021/03/27/3-SQL%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '2-SQL语言中的函数',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-03-28 14:58:20'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><link rel="stylesheet" href="/css/background.css"><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Ywrby个人博客网站" type="application/atom+xml">
</head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/ywrby.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">127</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">22</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> Bilibili</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/bangumis/"><i class="fa-fw fas fa-video"></i><span> 追番列表</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://space.bilibili.com/353923033"><i class="fa-fw fas fa-music"></i><span> B站主页</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/77593329_p0.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Ywrby个人博客网站</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> Bilibili</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/bangumis/"><i class="fa-fw fas fa-video"></i><span> 追番列表</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://space.bilibili.com/353923033"><i class="fa-fw fas fa-music"></i><span> B站主页</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">2-SQL语言中的函数</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2021-03-27T15:22:25.000Z" title="Created 2021-03-27 23:22:25">2021-03-27</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2021-03-28T06:58:20.000Z" title="Updated 2021-03-28 14:58:20">2021-03-28</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="2-SQL语言中的函数"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="SQL语言–函数"><a href="#SQL语言–函数" class="headerlink" title="SQL语言–函数"></a>SQL语言–函数</h1><h3 id="概念："><a href="#概念：" class="headerlink" title="概念："></a>概念：</h3><p>将一组逻辑语句封装在方法体内，对外暴露方法名</p>
<h3 id="优点："><a href="#优点：" class="headerlink" title="优点："></a>优点：</h3><p>隐藏了实现细节，提高了代码的重用性</p>
<h3 id="调用语法："><a href="#调用语法：" class="headerlink" title="调用语法："></a>调用语法：</h3><p>SELECT 函数名(实参列表) 【FROM 表】;</p>
<p>特点：<br>函数名与函数功能</p>
<h3 id="分类："><a href="#分类：" class="headerlink" title="分类："></a>分类：</h3><ol>
<li>单行函数，例如:CONCAT(str1,str2,…),LENGTH(str)等等</li>
<li>分组函数（也叫统计函数，聚合函数，组函数），做统计使用</li>
</ol>
<hr>
<h3 id="字符函数"><a href="#字符函数" class="headerlink" title="字符函数"></a>字符函数</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"># 常见函数</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">概念：将一组逻辑语句封装在方法体内，对外暴露方法名</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">优点：隐藏了实现细节，提高了代码的重用性</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">调用语法：</span></span><br><span class="line"><span class="comment">SELECT 函数名(实参列表) 【FROM 表】;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">函数名与函数功能</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">1. 单行函数，例如:CONCAT(str1,str2,...),LENGTH(str)等等</span></span><br><span class="line"><span class="comment">2. 分组函数（也叫统计函数，聚合函数，组函数），做统计使用</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 单行函数又分为：字符函数，数学函数，日期函数，其他函数，流程控制函数</span><br><span class="line"></span><br><span class="line"># 字符函数</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># LENGTH(str)函数返回字节长度，字节长度取决于编码方式</span><br><span class="line"># utf8中一个字母占一个字节，一个汉字占<span class="number">3</span>个字节</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(<span class="string">&#x27;John&#x27;</span>);</span><br><span class="line"><span class="keyword">SELECT</span> LENGTH(<span class="string">&#x27;张三&#x27;</span>);  </span><br><span class="line"></span><br><span class="line">#CONCAT(str1,str2,...) 用于拼接字符串</span><br><span class="line"><span class="keyword">SELECT</span> CONCAT(last_name,<span class="string">&#x27; &#x27;</span>,first_name) 姓名 <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># 转大小写</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">UPPER</span>(<span class="string">&#x27;Leslie&#x27;</span>);</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">LOWER</span>(<span class="string">&#x27;Leslie&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># SUBSTR(str <span class="keyword">FROM</span> pos <span class="keyword">FOR</span> len),截取部分字符串(<span class="keyword">SQL</span>语言中索引从<span class="number">1</span>开始)</span><br><span class="line"><span class="keyword">SELECT</span> SUBSTR(<span class="string">&#x27;Hello World&#x27;</span>,<span class="number">7</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;</span><br><span class="line"></span><br><span class="line"># 注意这里第二个参数是截取开始的位置，第三个参数是字符长度，不是结束截取的位置</span><br><span class="line"><span class="keyword">SELECT</span> SUBSTR(<span class="string">&#x27;Hello World&#x27;</span>,<span class="number">1</span>,<span class="number">5</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;  </span><br><span class="line"></span><br><span class="line"># INSTR(str,substr)  ,返回子字符串在原字符串中的索引</span><br><span class="line"><span class="keyword">SELECT</span> INSTR(<span class="string">&#x27;Hello World&#x27;</span>,<span class="string">&#x27;Wor&#x27;</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;</span><br><span class="line"></span><br><span class="line"># <span class="built_in">TRIM</span>([&#123;<span class="keyword">BOTH</span> <span class="operator">|</span> <span class="keyword">LEADING</span> <span class="operator">|</span> <span class="keyword">TRAILING</span>&#125; [remstr] <span class="keyword">FROM</span>] str) 取出左右空格</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">TRIM</span>(<span class="string">&#x27;    Hello     &#x27;</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">TRIM</span>(<span class="string">&#x27;a&#x27;</span> <span class="keyword">FROM</span> <span class="string">&#x27;aaaaaaaHELaaaaaaLOaaaaaaaa&#x27;</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;  # 指定要去掉的字符，并且只去掉左右两端</span><br><span class="line"></span><br><span class="line"># LPAD(str,len,padstr)  ,左填充函数，第一个参数是目标字符串，第二个参数是最终长度，第三个参数是填充字符</span><br><span class="line"># 填充字符可以是多个字符，与此对应同样有右填充RPAD</span><br><span class="line"><span class="keyword">SELECT</span> LPAD(<span class="string">&#x27;HELLO&#x27;</span>,<span class="number">10</span>,<span class="string">&#x27;*&#x27;</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;</span><br><span class="line"></span><br><span class="line"># REPLACE(str,from_str,to_str)  替换函数（替换所有）</span><br><span class="line"><span class="keyword">SELECT</span> REPLACE(<span class="string">&#x27;Hello World&#x27;</span>,<span class="string">&#x27;World&#x27;</span>,<span class="string">&#x27;Leslie&#x27;</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;</span><br></pre></td></tr></table></figure>


<h3 id="数学函数"><a href="#数学函数" class="headerlink" title="数学函数"></a>数学函数</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 数学函数</span><br><span class="line"></span><br><span class="line"># ROUND(X)  四舍五入</span><br><span class="line"><span class="keyword">SELECT</span> ROUND(<span class="number">4.555</span>);</span><br><span class="line"><span class="keyword">SELECT</span> ROUND(<span class="number">1.567</span>,<span class="number">2</span>);  # 小数点后保留两位</span><br><span class="line"></span><br><span class="line"># <span class="built_in">CEIL</span>(X) 向上取整</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">CEIL</span>(<span class="number">1.05</span>);</span><br><span class="line"># <span class="built_in">FLOOR</span>(X) 向下取整</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">FLOOR</span>(<span class="number">1.05</span>);</span><br><span class="line"></span><br><span class="line"># <span class="keyword">TRUNCATE</span>(X,D)  截断函数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">TRUNCATE</span>(<span class="number">1.69999</span>,<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"># <span class="built_in">MOD</span>(N,M)  取余函数  等价于<span class="operator">%</span> 本质是 <span class="built_in">MOD</span>(a,b)等价于a<span class="operator">-</span>a<span class="operator">/</span>b<span class="operator">*</span>a;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MOD</span>(<span class="number">10</span>,<span class="number">1</span>);</span><br></pre></td></tr></table></figure>

<h3 id="日期函数"><a href="#日期函数" class="headerlink" title="日期函数"></a>日期函数</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 日期函数</span><br><span class="line"></span><br><span class="line"># NOW() 返回当前系统日期<span class="operator">+</span>时间</span><br><span class="line"><span class="keyword">SELECT</span> NOW();</span><br><span class="line"></span><br><span class="line"># CURDATE() 返回当前系统日期，不包含时间</span><br><span class="line"><span class="keyword">SELECT</span> CURDATE();</span><br><span class="line"></span><br><span class="line"># CURTIME() 返回当前系统时间，不返回日期</span><br><span class="line"><span class="keyword">SELECT</span> CURTIME();</span><br><span class="line"></span><br><span class="line"># 获取指定部分的日，月，年等信息(小时，分钟，秒)</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">YEAR</span>(NOW());</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">MONTH</span>(<span class="string">&#x27;1998-1-1&#x27;</span>);</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DAY</span>(<span class="string">&#x27;2020-12-21&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># STR_TO_DATE(str,format) 将日期格式字符转换为指定个式的日期</span><br><span class="line"><span class="keyword">SELECT</span> STR_TO_DATE(<span class="string">&#x27;03-11-2019&#x27;</span>,<span class="string">&#x27;%d-%m-%Y&#x27;</span>) <span class="keyword">AS</span> 日期;</span><br><span class="line"></span><br><span class="line"># 查询入职日期为<span class="number">1992</span><span class="number">-4</span><span class="number">-3</span>的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> hiredate<span class="operator">=</span>STR_TO_DATE(<span class="string">&#x27;4-3 1992&#x27;</span>,<span class="string">&#x27;%m-%d %Y&#x27;</span>);</span><br><span class="line"></span><br><span class="line"># DATE_FORMAT(<span class="type">date</span>,format)  将日期转换成字符</span><br><span class="line"><span class="keyword">SELECT</span> DATE_FORMAT(NOW(),<span class="string">&#x27;%Y年%m月%d日--%H时%i分钟%s秒&#x27;</span>) <span class="keyword">AS</span> 当前日期;</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>格式符</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td>%Y</td>
<td>四位的年份</td>
</tr>
<tr>
<td>%y</td>
<td>两位的年份</td>
</tr>
<tr>
<td>%m</td>
<td>月份（01，02，03…,12）</td>
</tr>
<tr>
<td>%c</td>
<td>月份（1，2，3…12）</td>
</tr>
<tr>
<td>%d</td>
<td>日（01，02，03…）</td>
</tr>
<tr>
<td>%H</td>
<td>小时（二十四小时制）</td>
</tr>
<tr>
<td>%h</td>
<td>小时（十二小时制）</td>
</tr>
<tr>
<td>%i</td>
<td>分钟（00，01…59）</td>
</tr>
<tr>
<td>%s</td>
<td>秒（00，01…59）</td>
</tr>
</tbody></table>
<hr>
<h3 id="其他函数"><a href="#其他函数" class="headerlink" title="其他函数"></a>其他函数</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 其他函数</span><br><span class="line"></span><br><span class="line"># VERSION() 查看当前版本号</span><br><span class="line"><span class="keyword">SELECT</span> VERSION();</span><br><span class="line"># DATABASE() 查看当前所在的库</span><br><span class="line"><span class="keyword">SELECT</span> DATABASE();</span><br><span class="line"># <span class="keyword">USER</span>() 查询当前用户</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">USER</span>();</span><br></pre></td></tr></table></figure>

<h3 id="流程控制函数"><a href="#流程控制函数" class="headerlink" title="流程控制函数"></a>流程控制函数</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"># 流程控制函数</span><br><span class="line"></span><br><span class="line"># IF(expr1,expr2,expr3) 实现类似if...else的效果</span><br><span class="line"># 类似于三元运算符，表达式<span class="number">1</span>的值成立返回表达式<span class="number">2</span>的值，否则返回表达式<span class="number">3</span>的值</span><br><span class="line"><span class="keyword">SELECT</span> IF(<span class="string">&#x27;10&gt;5&#x27;</span>,<span class="string">&#x27;大于&#x27;</span>,<span class="string">&#x27;小于&#x27;</span>) <span class="keyword">AS</span> <span class="keyword">result</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># <span class="keyword">CASE</span>函数，实现类似于switch...case效果</span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">格式</span></span><br><span class="line"><span class="comment">CASE case_value</span></span><br><span class="line"><span class="comment">	WHEN when_value THEN</span></span><br><span class="line"><span class="comment">		statement_list</span></span><br><span class="line"><span class="comment">	ELSE</span></span><br><span class="line"><span class="comment">		statement_list</span></span><br><span class="line"><span class="comment">END CASE;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 查询员工工资，要求部门号<span class="operator">=</span><span class="number">30</span>的显示的工资为<span class="number">1.1</span>倍</span><br><span class="line"># 部门号为<span class="number">40</span>的，显示的工资为<span class="number">1.2</span>倍，其他部门原价显示</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	department_id,</span><br><span class="line">	salary 工资,</span><br><span class="line"><span class="keyword">CASE</span> department_id</span><br><span class="line">	<span class="keyword">WHEN</span> <span class="number">30</span> <span class="keyword">THEN</span></span><br><span class="line">		salary<span class="operator">*</span><span class="number">1.1</span></span><br><span class="line">	<span class="keyword">WHEN</span> <span class="number">40</span> <span class="keyword">THEN</span></span><br><span class="line">		salary<span class="operator">*</span><span class="number">1.2</span></span><br><span class="line">	<span class="keyword">ELSE</span> salary</span><br><span class="line"><span class="keyword">END</span> <span class="keyword">AS</span> 新工资 </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees;</span><br><span class="line">	</span><br><span class="line"># <span class="keyword">case</span>函数的使用二</span><br><span class="line"># 工资大于<span class="number">20000</span>，显示A级别</span><br><span class="line"># 工资大于<span class="number">15000</span>，显示B级别</span><br><span class="line"># 工资大于<span class="number">10000</span>，显示C级别</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	last_name,</span><br><span class="line">	salary,</span><br><span class="line"><span class="keyword">CASE</span> </span><br><span class="line">	<span class="keyword">WHEN</span> salary<span class="operator">&gt;</span><span class="number">20000</span> <span class="keyword">THEN</span> <span class="string">&#x27;A&#x27;</span></span><br><span class="line">	<span class="keyword">WHEN</span> salary<span class="operator">&gt;</span><span class="number">15000</span> <span class="keyword">THEN</span> <span class="string">&#x27;B&#x27;</span></span><br><span class="line">	<span class="keyword">WHEN</span> salary<span class="operator">&gt;</span><span class="number">10000</span> <span class="keyword">THEN</span> <span class="string">&#x27;C&#x27;</span></span><br><span class="line">	<span class="keyword">ELSE</span> <span class="string">&#x27;D&#x27;</span></span><br><span class="line"><span class="keyword">END</span> <span class="keyword">AS</span> 工资等级</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">	employees;</span><br></pre></td></tr></table></figure>

<hr>
<hr>
<h2 id="分组函数"><a href="#分组函数" class="headerlink" title="分组函数"></a>分组函数</h2><h3 id="分类：-1"><a href="#分类：-1" class="headerlink" title="分类："></a>分类：</h3><p>sum 求和，avg 平均值，max 最大值，min 最小值</p>
<h3 id="特点："><a href="#特点：" class="headerlink" title="特点："></a>特点：</h3><ol>
<li>sum,avg可以处理数值型数据，max,min,count可以处理任何类型数据</li>
<li>以上几个分组函数都会自动忽略null值</li>
<li>可以和distinct搭配使用</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"># 分组函数</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">分类：sum 求和，avg 平均值，max 最大值，min 最小值</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">1. sum,avg可以处理数值型数据，max,min,count可以处理任何类型数据</span></span><br><span class="line"><span class="comment">2. 以上几个分组函数都会自动忽略null值</span></span><br><span class="line"><span class="comment">3. 可以和distinct搭配使用</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># sum求和</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">SUM</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"># avg求平均值</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"># max最大值</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"># min最小值</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># count统计有效数据个数（非<span class="keyword">null</span>值）</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 和<span class="keyword">distinct</span>搭配使用,取出重复内容</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">SUM</span>(<span class="keyword">DISTINCT</span> salary),<span class="built_in">SUM</span>(salary) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br><span class="line"># <span class="built_in">count</span>()函数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> employees;  # 可以用来统计实际有效行数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="number">1</span>) <span class="keyword">FROM</span> employees;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="分组查询"><a href="#分组查询" class="headerlink" title="分组查询"></a>分组查询</h2><h3 id="语法："><a href="#语法：" class="headerlink" title="语法："></a>语法：</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 分组函数,列（要求出现在group_by后面）</span><br><span class="line"><span class="keyword">FROM</span> 表</span><br><span class="line">【<span class="keyword">WHERE</span> 筛选条件】</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> 分组列表</span><br><span class="line">【<span class="keyword">ORDER</span> <span class="keyword">BY</span> 子句】</span><br></pre></td></tr></table></figure>


<h3 id="注意："><a href="#注意：" class="headerlink" title="注意："></a>注意：</h3><p>查询列表比较特殊，要求是分组函数和group_by后出现的字段</p>
<h3 id="分组查询中的筛选可以分为两类"><a href="#分组查询中的筛选可以分为两类" class="headerlink" title="分组查询中的筛选可以分为两类"></a>分组查询中的筛选可以分为两类</h3><ol>
<li><strong>分组前的筛选</strong>：分组前的筛选也就是筛选的内容在数据库中就存在，<br>可以直接利用对应列筛选，利用where语句筛选，位置在group_by字句的前面</li>
<li><strong>分组后的筛选</strong>：分组后的筛选是利用已经重新分配的组内的信息进行筛选，这些信息不直接存储于数据库中。利用having语句筛选，位置在group_by字句的后面</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"># 分组查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">SELECT 分组函数,列（要求出现在group_by后面）</span></span><br><span class="line"><span class="comment">FROM 表</span></span><br><span class="line"><span class="comment">【WHERE 筛选条件】</span></span><br><span class="line"><span class="comment">GROUP BY 分组列表</span></span><br><span class="line"><span class="comment">【ORDER BY 子句】</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">注意：查询列表比较特殊，要求是分组函数和group_by后出现的字段</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">分组查询中的筛选可以分为两类</span></span><br><span class="line"><span class="comment">1. 分组前的筛选：分组前的筛选也就是筛选的内容在数据库中就存在，</span></span><br><span class="line"><span class="comment">可以直接利用对应列筛选，利用where语句筛选，位置在group_by字句的前面</span></span><br><span class="line"><span class="comment">2. 分组后的筛选：分组后的筛选是利用已经重新分配的组内的信息进行筛选，</span></span><br><span class="line"><span class="comment">这些信息不直接存储于数据库中。利用having语句筛选，位置在group_by字句的后面</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 查询每个部门平均工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) <span class="keyword">AS</span> 平均工资,job_id <span class="keyword">AS</span> 部门</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id;</span><br><span class="line"></span><br><span class="line"># 查询每个位置上的部门个数</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) ,location_id</span><br><span class="line"><span class="keyword">FROM</span> departments</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> location_id; </span><br><span class="line"></span><br><span class="line"># 添加筛选条件</span><br><span class="line"># 查询邮箱中包含A字符的，每个部门的平均工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary),department_id</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%A%&#x27;</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id;</span><br><span class="line"></span><br><span class="line"># 查询哪个部门员工个数大于<span class="number">2</span>（添加分组后的筛选）</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">这里不是利用employees表中的原数据进行筛选，</span></span><br><span class="line"><span class="comment">而是根据筛选后的结果进行二次筛选，</span></span><br><span class="line"><span class="comment">所以不能再用where关键字，而是在最后追加having关键字</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) ,department_id</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id</span><br><span class="line"><span class="keyword">HAVING</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)<span class="operator">&gt;</span><span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 查询每个部门有奖金的最高工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) ,job_id</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> commission_pct <span class="keyword">IS</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> job_id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 查询领导编号大于<span class="number">102</span>，并且其手下最低工资大于<span class="number">5000</span>的领导</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary) ,manager_id</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> manager_id<span class="operator">&gt;</span><span class="number">102</span></span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> manager_id</span><br><span class="line"><span class="keyword">HAVING</span> <span class="built_in">MIN</span>(salary)<span class="operator">&gt;</span><span class="number">5000</span>;</span><br></pre></td></tr></table></figure>



<h2 id="连接查询"><a href="#连接查询" class="headerlink" title="连接查询"></a>连接查询</h2><h3 id="含义："><a href="#含义：" class="headerlink" title="含义："></a>含义：</h3><p>又称为多表查询，当查询的字段来自于多个表时，就会用到连接查询</p>
<h3 id="分类：-2"><a href="#分类：-2" class="headerlink" title="分类："></a>分类：</h3><h4 id="按年代分类："><a href="#按年代分类：" class="headerlink" title="按年代分类："></a>按年代分类：</h4><ul>
<li>sql92标准（仅支持内连接）</li>
<li>sql99标准（除了全外连接外都支持）【推荐】<h4 id="按功能分类："><a href="#按功能分类：" class="headerlink" title="按功能分类："></a>按功能分类：</h4></li>
<li>内连接（包括等值连接，非等值连接，子连接）</li>
<li>外连接（包括左外连接，右外连接，全外连接）</li>
<li>交叉连接</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br></pre></td><td class="code"><pre><span class="line"># DQL函数语句<span class="comment">--连接查询</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">含义：又称为多表查询，当查询的字段来自于多个表时，就会用到连接查询</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">按年代分类：sql92标准（仅支持内连接），sql99标准（除了全外连接外都支持）【推荐】</span></span><br><span class="line"><span class="comment">按功能分类：内连接（包括等值连接，非等值连接，子连接）</span></span><br><span class="line"><span class="comment">						外连接（包括左外连接，右外连接，全外连接）</span></span><br><span class="line"><span class="comment">						交叉连接</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">笛卡尔乘积现象</span></span><br><span class="line"><span class="comment">不利用连接查询，而贸然利用两个表格数据匹配结果，不添加连接条件。最终得到的结果是表一和表二的完全匹配</span></span><br><span class="line"><span class="comment">例如想要通过boys表和beauty表匹配对象，假如写作下式</span></span><br><span class="line"><span class="comment">SELECT NAME,boyName FROM boys,beauty;</span></span><br><span class="line"><span class="comment">最终得到4*12=48条数据，这与我们想要的结果不符，而只是单纯的完全匹配</span></span><br><span class="line"><span class="comment">所以我们需要连接查询</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line">#错误格式</span><br><span class="line"><span class="keyword">SELECT</span> NAME,boyName <span class="keyword">FROM</span> boys,beauty;</span><br><span class="line">#正确格式</span><br><span class="line"><span class="keyword">SELECT</span> NAME,boyName </span><br><span class="line"><span class="keyword">FROM</span> boys,beauty</span><br><span class="line"><span class="keyword">WHERE</span> beauty.boyfriend_id<span class="operator">=</span>boys.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 等值连接</span><br><span class="line"><span class="keyword">SELECT</span> NAME,boyName </span><br><span class="line"><span class="keyword">FROM</span> boys,beauty</span><br><span class="line"><span class="keyword">WHERE</span> beauty.boyfriend_id<span class="operator">=</span>boys.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 起别名后进行等值连接</span><br><span class="line"># 起别名后在<span class="keyword">select</span>语句中的表名也需要修改为别名</span><br><span class="line"><span class="keyword">SELECT</span> last_name,e.job_id,job_title</span><br><span class="line"><span class="keyword">FROM</span> employees e,jobs j</span><br><span class="line"><span class="keyword">WHERE</span> e.`job_id`<span class="operator">=</span>j.`job_id` ;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 等值查询后可以进行模糊查询等，用<span class="keyword">AND</span>语句连接即可</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 非等值连接</span><br><span class="line"># 相较于等值查询的主要区别就是替换了查询语句的等于为其他判断符号</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 自连接</span><br><span class="line"># 本质就是只在自己表内部的等值连接</span><br><span class="line"># 查询 员工名和上级名称</span><br><span class="line"><span class="keyword">SELECT</span> e.employee_id,e.last_name,m.employee_id,m.last_name</span><br><span class="line"><span class="keyword">FROM</span> employees e,employees m</span><br><span class="line"><span class="keyword">WHERE</span> e.`manager_id`<span class="operator">=</span>m.`employee_id`;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># sql99语法</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">SELECT 查询列表</span></span><br><span class="line"><span class="comment">FROM 表1 别名 【连接类型】</span></span><br><span class="line"><span class="comment">JOIN 表2 别名</span></span><br><span class="line"><span class="comment">ON 连接条件</span></span><br><span class="line"><span class="comment">【WHERE 筛选条件】</span></span><br><span class="line"><span class="comment">【GROUP BY 分组】</span></span><br><span class="line"><span class="comment">【HAVING 筛选条件】</span></span><br><span class="line"><span class="comment">【ORDER BY 排序列表】</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">sql99语法包括（连接类型）：</span></span><br><span class="line"><span class="comment">内连接(INNER)</span></span><br><span class="line"><span class="comment">外连接</span></span><br><span class="line"><span class="comment">		左外(LEFT 【OUTER】)</span></span><br><span class="line"><span class="comment">		右外(LEFT 【OUTER】)</span></span><br><span class="line"><span class="comment">		全外(FULL 【OUTER】)</span></span><br><span class="line"><span class="comment">交叉连接(CROSS)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 内连接</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">SELECT 查询列表</span></span><br><span class="line"><span class="comment">FROM 表1 别名</span></span><br><span class="line"><span class="comment">INNER JOIN 表2 别名</span></span><br><span class="line"><span class="comment">ON 连接条件;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">INNER可以省略</span></span><br><span class="line"><span class="comment">筛选条件放在where后面，连接条件放在on后面，提高分离性，便于阅读</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"># 等值连接</span><br><span class="line"></span><br><span class="line"># 查询部门名与员工名</span><br><span class="line"><span class="keyword">SELECT</span> last_name,department_name</span><br><span class="line"><span class="keyword">FROM</span> employees e</span><br><span class="line"><span class="keyword">INNER</span> <span class="keyword">JOIN</span> departments d  </span><br><span class="line"><span class="keyword">ON</span> e.`department_id`<span class="operator">=</span>d.`department_id`;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 外连接</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">用于查询一个表中有，另一个表中没有的记录</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">外连接的查询结果为主表中的所有记录</span></span><br><span class="line"><span class="comment">		如果表中有和它匹配，则显示匹配的值</span></span><br><span class="line"><span class="comment">		如果没有匹配值，则显示null</span></span><br><span class="line"><span class="comment">左外连接，left左边的是主表</span></span><br><span class="line"><span class="comment">右外连接，left右边的是主表</span></span><br><span class="line"><span class="comment">左外和右外交换顺序，可以达到相同的效果</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 左外连接</span><br><span class="line"><span class="keyword">SELECT</span> b.name,boy.<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span> beauty b</span><br><span class="line"><span class="keyword">LEFT</span> <span class="keyword">OUTER</span> <span class="keyword">JOIN</span> boys boy</span><br><span class="line"><span class="keyword">ON</span> b.`boyfriend_id`<span class="operator">=</span>boy.`id`;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 交叉连接</span><br><span class="line"># 本质就是笛卡尔乘积</span><br><span class="line"><span class="keyword">SELECT</span> b.<span class="operator">*</span>,boy.<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span> beauty b</span><br><span class="line"><span class="keyword">CROSS</span> <span class="keyword">JOIN</span> boys boy;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1uJ411k7wy?p=523">外连接</a></p>
<h2 id="子查询"><a href="#子查询" class="headerlink" title="子查询"></a>子查询</h2><h3 id="含义：-1"><a href="#含义：-1" class="headerlink" title="含义："></a>含义：</h3><p>出现在其他语句中的select语句，称为子查询或内查询<br>外部出现的查询语句，称为主查或外查询</p>
<h3 id="分类：-3"><a href="#分类：-3" class="headerlink" title="分类："></a>分类：</h3><h4 id="按子查询出现的位置："><a href="#按子查询出现的位置：" class="headerlink" title="按子查询出现的位置："></a>按子查询出现的位置：</h4><ul>
<li>SELECT 后面(仅支持标量子查询)</li>
<li>FROM 后面（支持表子查询）</li>
<li>WHERE或HAVING后面（标量子查询，列子查询，行子查询）</li>
<li>EXISTS 后面（表子查询）<h4 id="按结果集的行列数不同："><a href="#按结果集的行列数不同：" class="headerlink" title="按结果集的行列数不同："></a>按结果集的行列数不同：</h4></li>
<li>标量子查询（结果集只有一行一列）</li>
<li>列子查询（结果集只有一列多行）</li>
<li>行子查询（结果集有一行多列）</li>
<li>表子查询（结果集一般多行多列）</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br></pre></td><td class="code"><pre><span class="line"># 子查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">含义：</span></span><br><span class="line"><span class="comment">出现在其他语句中的select语句，称为子查询或内查询</span></span><br><span class="line"><span class="comment">外部出现的查询语句，称为主查或外查询</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">分类：</span></span><br><span class="line"><span class="comment">按子查询出现的位置：</span></span><br><span class="line"><span class="comment">		SELECT 后面(仅支持标量子查询)</span></span><br><span class="line"><span class="comment">		FROM 后面（支持表子查询）</span></span><br><span class="line"><span class="comment">		WHERE或HAVING 后面（标量子查询，列子查询，行子查询）</span></span><br><span class="line"><span class="comment">		EXISTS 后面（表子查询）</span></span><br><span class="line"><span class="comment">按结果集的行列数不同：</span></span><br><span class="line"><span class="comment">		标量子查询（结果集只有一行一列）</span></span><br><span class="line"><span class="comment">		列子查询（结果集只有一列多行）</span></span><br><span class="line"><span class="comment">		行子查询（结果集有一行多列）</span></span><br><span class="line"><span class="comment">		表子查询（结果集一般多行多列）</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># <span class="keyword">WHERE</span>或<span class="keyword">HAVING</span> 后面</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">1. 标量子查询（单行子查询）</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">2. 列子查询（多行子查询）</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">3. 行子查询（多行多列）</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">子查询放在小括号内</span></span><br><span class="line"><span class="comment">子查询一般放在条件右侧</span></span><br><span class="line"><span class="comment">标量子查询一般搭配单行操作符使用（&gt;,&lt;,=,&gt;=,&lt;=,&lt;&gt;等等）</span></span><br><span class="line"><span class="comment">列子查询一般搭配多行操作符使用（in,any/some,all）</span></span><br><span class="line"><span class="comment">子查询的执行优先于主查询，主查询的条件用到了子查询的结果</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 标量子查询</span><br><span class="line"></span><br><span class="line"># 查询谁的工资比Abel高</span><br><span class="line"># <span class="number">1.</span> 查询Abel工资（单行查询）</span><br><span class="line"><span class="keyword">SELECT</span> salary</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> last_name<span class="operator">=</span> <span class="string">&#x27;Abel&#x27;</span>;</span><br><span class="line"># <span class="number">2.</span>查询员工信息，满足salary大于<span class="number">1</span>中结果的</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> salary<span class="operator">&gt;</span>(</span><br><span class="line">	<span class="keyword">SELECT</span> salary</span><br><span class="line">	<span class="keyword">FROM</span> employees</span><br><span class="line">	<span class="keyword">WHERE</span> last_name<span class="operator">=</span> <span class="string">&#x27;Abel&#x27;</span></span><br><span class="line">);</span><br><span class="line"># 子查询内部不需要;作为结束标志</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 非法使用标量子查询</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(salary),department_id </span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id</span><br><span class="line"><span class="keyword">HAVING</span> <span class="built_in">MIN</span>(salary)<span class="operator">&gt;</span>(</span><br><span class="line">	<span class="keyword">SELECT</span> salary</span><br><span class="line">	<span class="keyword">FROM</span> employees</span><br><span class="line">	<span class="keyword">WHERE</span> department_id<span class="operator">=</span><span class="number">50</span></span><br><span class="line">);</span><br><span class="line"># 以上语句会报错，因为子查询语句的结果不为一行一列，所以不能用标量子查询（多行多列或<span class="number">0</span>行<span class="number">0</span>列都不可以）</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 列子查询（多行子查询，因为子查询结果是一列多行）</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">多行操作符：</span></span><br><span class="line"><span class="comment">IN/NOT IN   等于/不等于列表中的任意一个</span></span><br><span class="line"><span class="comment">ANY/SOME 和子查询中的某个值作比较，例如15&gt;ANY(40,10,25)，因为15&gt;10所以上式成立</span></span><br><span class="line"><span class="comment">ALL 和子查询返回的所有值比较，例如15&gt;ANY(40,10,25)，因为40&gt;15所以上式不成立</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 返回location_id是<span class="number">1400</span>或<span class="number">1700</span>的部门中的员工姓名</span><br><span class="line"># <span class="number">1.</span> location_id是<span class="number">1400</span>或<span class="number">1700</span>的部门号</span><br><span class="line"><span class="keyword">SELECT</span> department_id</span><br><span class="line"><span class="keyword">FROM</span> departments</span><br><span class="line"><span class="keyword">WHERE</span> location_id <span class="keyword">IN</span>(<span class="number">1400</span>,<span class="number">1700</span>);</span><br><span class="line"></span><br><span class="line"># <span class="number">2.</span> 查询符合条件的员工姓名</span><br><span class="line"><span class="keyword">SELECT</span> last_name</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> </span><br><span class="line">	department_id <span class="keyword">IN</span>(</span><br><span class="line">		<span class="keyword">SELECT</span> department_id</span><br><span class="line">		<span class="keyword">FROM</span> departments</span><br><span class="line">		<span class="keyword">WHERE</span> location_id <span class="keyword">IN</span>(<span class="number">1400</span>,<span class="number">1700</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 行子查询（一行多列或多行多列）</span><br><span class="line"></span><br><span class="line"># 查询员工编号最小并且工资最高的员工信息（不一定存在同时满足两个条件的员工）</span><br><span class="line"># <span class="number">1.</span> 查询最小的员工编号</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MIN</span>(employee_id)</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"># <span class="number">2.</span> 查询最高工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary)</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"># <span class="number">3.</span> 查询同时满足这两个条件的员工</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> employee_id<span class="operator">=</span>(</span><br><span class="line">	<span class="keyword">SELECT</span> <span class="built_in">MIN</span>(employee_id)</span><br><span class="line">	<span class="keyword">FROM</span> employees</span><br><span class="line">)<span class="keyword">AND</span>(</span><br><span class="line">	<span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary)</span><br><span class="line">	<span class="keyword">FROM</span> employees</span><br><span class="line">);</span><br><span class="line"># 利用行子查询代替上式</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">WHERE</span> (employee_id,salary)<span class="operator">=</span>(</span><br><span class="line">	<span class="keyword">SELECT</span> <span class="built_in">MIN</span>(employee_id),<span class="built_in">MAX</span>(salary)</span><br><span class="line">	<span class="keyword">FROM</span> employees</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 放在<span class="keyword">SELECT</span>后面</span><br><span class="line"></span><br><span class="line"># 查询每个部门员工个数</span><br><span class="line"><span class="keyword">SELECT</span> d.<span class="operator">*</span>,(</span><br><span class="line">	<span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>)</span><br><span class="line">	<span class="keyword">FROM</span> employees e</span><br><span class="line">	<span class="keyword">WHERE</span> e.`department_id`<span class="operator">=</span>d.`department_id`</span><br><span class="line">)</span><br><span class="line"><span class="keyword">FROM</span> departments d;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 放在<span class="keyword">from</span>后面</span><br><span class="line"></span><br><span class="line"># 查询每个部门的平均工资等级</span><br><span class="line"># <span class="number">1.</span> 查询每个部门的平均工资</span><br><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary),department_id</span><br><span class="line"><span class="keyword">FROM</span> employees</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id;</span><br><span class="line"># <span class="number">2.</span> 查询工资等级</span><br><span class="line"><span class="keyword">SELECT</span> ag_dep.<span class="operator">*</span>,g.`grade_leve`</span><br><span class="line"><span class="keyword">FROM</span> (</span><br><span class="line">	<span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) ag,department_id</span><br><span class="line">	<span class="keyword">FROM</span> employees</span><br><span class="line">	<span class="keyword">GROUP</span> <span class="keyword">BY</span> department_id</span><br><span class="line">) ag_dep  # 起别名</span><br><span class="line"><span class="keyword">INNER</span> <span class="keyword">JOIN</span> job_grades g</span><br><span class="line"><span class="keyword">ON</span> ag_dep.ag <span class="keyword">BETWEEN</span> lowest_sal <span class="keyword">AND</span> highest_sal; </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># <span class="keyword">exists</span>后面（相关子查询）</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">语法：EXISTS(完整查询语句)</span></span><br><span class="line"><span class="comment">结果只有0或1</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"># 判断employees中是否存在employee_id这一列</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">EXISTS</span>(<span class="keyword">SELECT</span> employee_id <span class="keyword">FROM</span> employees);</span><br><span class="line"># 判断是否存在工资为<span class="number">30000</span>的人</span><br><span class="line"><span class="keyword">SELECT</span> <span class="keyword">EXISTS</span>(<span class="keyword">SELECT</span> employee_id <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> salary<span class="operator">=</span><span class="number">30000</span>);</span><br></pre></td></tr></table></figure>


<h2 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h2><h3 id="应用场景："><a href="#应用场景：" class="headerlink" title="应用场景："></a>应用场景：</h3><p>当要显示的数据，一夜显示不全，需要分页提交sql请求</p>
<p>LIMIT 语句放在查询语句的最后</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"># 分页查询</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">应用场景：</span></span><br><span class="line"><span class="comment">当要显示的数据，一夜显示不全，需要分页提交sql请求</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">SELECT 查询列表</span></span><br><span class="line"><span class="comment">FROM 表名</span></span><br><span class="line"><span class="comment">【JOIN TYPE join 表2</span></span><br><span class="line"><span class="comment">ON 连接条件</span></span><br><span class="line"><span class="comment">WHERE 筛选条件</span></span><br><span class="line"><span class="comment">GROUP BY 分组字段</span></span><br><span class="line"><span class="comment">HAVING 分组后的筛选</span></span><br><span class="line"><span class="comment">ORDER BY 排序的字段】</span></span><br><span class="line"><span class="comment">LIMIT offset,zize;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">offset表示条目的起始索引（起始索引从0开始）</span></span><br><span class="line"><span class="comment">size表示要显示的条目个数</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">LIMIT 语句放在查询语句的最后</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employees LIMIT <span class="number">0</span>,<span class="number">5</span>;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employees LIMIT <span class="number">10</span>,<span class="number">15</span>;</span><br></pre></td></tr></table></figure>


<h2 id="联合查询"><a href="#联合查询" class="headerlink" title="联合查询"></a>联合查询</h2><h3 id="union查询："><a href="#union查询：" class="headerlink" title="union查询："></a>union查询：</h3><p>将多条查询语句的结果合并成一个结果</p>
<h3 id="应用场景：-1"><a href="#应用场景：-1" class="headerlink" title="应用场景："></a>应用场景：</h3><p>要查询结果来自多个表，且多个表没有直接的连接关系，单查询的信息一致</p>
<h3 id="特点：-1"><a href="#特点：-1" class="headerlink" title="特点："></a>特点：</h3><p>要求多条查询语句的查询列数是一致的<br>要求多条查询语句每一列的类型和顺序最好是一致的<br>UNION关键字会自动去重，如果不想去重可以使用UNION ALL关键字</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"># 联合查询</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">union查询：将多条查询语句的结果合并成一个结果</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">语法：</span></span><br><span class="line"><span class="comment">查询语句</span></span><br><span class="line"><span class="comment">union</span></span><br><span class="line"><span class="comment">查询语句</span></span><br><span class="line"><span class="comment">...</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">应用场景：要查询结果来自多个表，且多个表没有直接的连接关系，单查询的信息一致</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">特点：</span></span><br><span class="line"><span class="comment">要求多条查询语句的查询列数是一致的</span></span><br><span class="line"><span class="comment">要求多条查询语句每一列的类型和顺序最好是一致的</span></span><br><span class="line"><span class="comment">UNION关键字会自动去重，如果不想去重可以使用UNION ALL关键字</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"># 查询部门编号<span class="operator">&gt;</span><span class="number">90</span>或邮箱中包含a的员工信息</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span> <span class="keyword">OR</span> department_id<span class="operator">&gt;</span><span class="number">90</span>;</span><br><span class="line"># 用联合查询来完成</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> email <span class="keyword">LIKE</span> <span class="string">&#x27;%a%&#x27;</span> </span><br><span class="line"><span class="keyword">UNION</span> <span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employees <span class="keyword">WHERE</span> department_id<span class="operator">&gt;</span><span class="number">90</span>;</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Ywrby</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://ywrby.cn/2021/03/27/3-SQL%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0/">http://ywrby.cn/2021/03/27/3-SQL%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/MySQL/">MySQL</a></div><div class="post_share"><div class="social-share" data-image="/img/77593329_p0.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/03/28/4-DML%E6%95%B0%E6%8D%AE%E6%93%8D%E6%8E%A7%E8%AF%AD%E8%A8%80/"><img class="prev-cover" src="/img/66150863_p0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">4-DML数据操控语言</div></div></a></div><div class="next-post pull-right"><a href="/2021/03/27/2-DQL%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80/"><img class="next-cover" src="/img/72124553_p0.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">2-DQL数据查询语言</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2021/03/27/1-MySQL%E7%AE%80%E8%BF%B0/" title="1-MySQL简述"><img class="cover" src="/img/73870940_p0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-27</div><div class="title">1-MySQL简述</div></div></a></div><div><a href="/2021/04/04/10-%E5%8F%98%E9%87%8F/" title="10-变量"><img class="cover" src="/img/72124553_p0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-04</div><div class="title">10-变量</div></div></a></div><div><a href="/2021/04/04/11-%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B%E5%92%8C%E5%87%BD%E6%95%B0/" title="11-存储过程和函数"><img class="cover" src="/img/77593329_p0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-04</div><div class="title">11-存储过程和函数</div></div></a></div><div><a href="/2021/04/04/12-%E5%88%86%E6%94%AF%E7%BB%93%E6%9E%84%EF%BC%8C%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84/" title="12-分支结构，循环结构"><img class="cover" src="/img/81691339_p0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-04</div><div class="title">12-分支结构，循环结构</div></div></a></div><div><a href="/2021/04/04/13-DCL%E7%94%A8%E6%88%B7%E7%AE%A1%E7%90%86%E8%AF%AD%E8%A8%80/" title="13-DCL用户管理语言"><img class="cover" src="/img/72581222_p0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-04</div><div class="title">13-DCL用户管理语言</div></div></a></div><div><a href="/2021/04/18/14-%E5%85%B3%E7%B3%BB%E4%BB%A3%E6%95%B0Relation-Algebra/" title="14-关系代数Relation Algebra"><img class="cover" src="/img/73870940_p0.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-04-18</div><div class="title">14-关系代数Relation Algebra</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/ywrby.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Ywrby</div><div class="author-info__description">Students majoring in CS at SWU</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">127</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">22</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/ywrby"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="mailto:ywrby0214@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://space.bilibili.com/353923033" target="_blank" title="Bilibili"><i class="fas fa-file-video"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=2278431384&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="https://twitter.com/ywrby1" target="_blank" title="Twitter"><i class="fab fa-twitter"></i></a><a class="social-icon" href="https://www.facebook.com/profile.php?id=100033741068822" target="_blank" title="Facebook"><i class="fab fa-facebook"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>Announcement</span></div><div class="announcement_content">Less is more</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#SQL%E8%AF%AD%E8%A8%80%E2%80%93%E5%87%BD%E6%95%B0"><span class="toc-number">1.</span> <span class="toc-text">SQL语言–函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A6%82%E5%BF%B5%EF%BC%9A"><span class="toc-number">1.0.1.</span> <span class="toc-text">概念：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BC%98%E7%82%B9%EF%BC%9A"><span class="toc-number">1.0.2.</span> <span class="toc-text">优点：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B0%83%E7%94%A8%E8%AF%AD%E6%B3%95%EF%BC%9A"><span class="toc-number">1.0.3.</span> <span class="toc-text">调用语法：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%B1%BB%EF%BC%9A"><span class="toc-number">1.0.4.</span> <span class="toc-text">分类：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.5.</span> <span class="toc-text">字符函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E5%AD%A6%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.6.</span> <span class="toc-text">数学函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.7.</span> <span class="toc-text">日期函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%B6%E4%BB%96%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.8.</span> <span class="toc-text">其他函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.9.</span> <span class="toc-text">流程控制函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E7%BB%84%E5%87%BD%E6%95%B0"><span class="toc-number">1.1.</span> <span class="toc-text">分组函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%B1%BB%EF%BC%9A-1"><span class="toc-number">1.1.1.</span> <span class="toc-text">分类：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%89%B9%E7%82%B9%EF%BC%9A"><span class="toc-number">1.1.2.</span> <span class="toc-text">特点：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.2.</span> <span class="toc-text">分组查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%AD%E6%B3%95%EF%BC%9A"><span class="toc-number">1.2.1.</span> <span class="toc-text">语法：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%EF%BC%9A"><span class="toc-number">1.2.2.</span> <span class="toc-text">注意：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2%E4%B8%AD%E7%9A%84%E7%AD%9B%E9%80%89%E5%8F%AF%E4%BB%A5%E5%88%86%E4%B8%BA%E4%B8%A4%E7%B1%BB"><span class="toc-number">1.2.3.</span> <span class="toc-text">分组查询中的筛选可以分为两类</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.3.</span> <span class="toc-text">连接查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AB%E4%B9%89%EF%BC%9A"><span class="toc-number">1.3.1.</span> <span class="toc-text">含义：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%B1%BB%EF%BC%9A-2"><span class="toc-number">1.3.2.</span> <span class="toc-text">分类：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%89%E5%B9%B4%E4%BB%A3%E5%88%86%E7%B1%BB%EF%BC%9A"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">按年代分类：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%89%E5%8A%9F%E8%83%BD%E5%88%86%E7%B1%BB%EF%BC%9A"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">按功能分类：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.4.</span> <span class="toc-text">子查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AB%E4%B9%89%EF%BC%9A-1"><span class="toc-number">1.4.1.</span> <span class="toc-text">含义：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E7%B1%BB%EF%BC%9A-3"><span class="toc-number">1.4.2.</span> <span class="toc-text">分类：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%89%E5%AD%90%E6%9F%A5%E8%AF%A2%E5%87%BA%E7%8E%B0%E7%9A%84%E4%BD%8D%E7%BD%AE%EF%BC%9A"><span class="toc-number">1.4.2.1.</span> <span class="toc-text">按子查询出现的位置：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%89%E7%BB%93%E6%9E%9C%E9%9B%86%E7%9A%84%E8%A1%8C%E5%88%97%E6%95%B0%E4%B8%8D%E5%90%8C%EF%BC%9A"><span class="toc-number">1.4.2.2.</span> <span class="toc-text">按结果集的行列数不同：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.5.</span> <span class="toc-text">分页查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF%EF%BC%9A"><span class="toc-number">1.5.1.</span> <span class="toc-text">应用场景：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-number">1.6.</span> <span class="toc-text">联合查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#union%E6%9F%A5%E8%AF%A2%EF%BC%9A"><span class="toc-number">1.6.1.</span> <span class="toc-text">union查询：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF%EF%BC%9A-1"><span class="toc-number">1.6.2.</span> <span class="toc-text">应用场景：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%89%B9%E7%82%B9%EF%BC%9A-1"><span class="toc-number">1.6.3.</span> <span class="toc-text">特点：</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/12/25/22-%E5%86%85%E5%AD%98%E7%A9%BA%E9%97%B4%E6%89%A9%E5%85%85%EF%BC%88%E8%A6%86%E7%9B%96%E4%B8%8E%E4%BA%A4%E6%8D%A2%EF%BC%89/" title="22-内存空间扩充（覆盖与交换）"><img src="/img/67888138_p0.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="22-内存空间扩充（覆盖与交换）"/></a><div class="content"><a class="title" href="/2021/12/25/22-%E5%86%85%E5%AD%98%E7%A9%BA%E9%97%B4%E6%89%A9%E5%85%85%EF%BC%88%E8%A6%86%E7%9B%96%E4%B8%8E%E4%BA%A4%E6%8D%A2%EF%BC%89/" title="22-内存空间扩充（覆盖与交换）">22-内存空间扩充（覆盖与交换）</a><time datetime="2021-12-25T08:15:49.000Z" title="Created 2021-12-25 16:15:49">2021-12-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/19/21-%E5%86%85%E5%AD%98%E4%B8%8E%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" title="21-内存与内存管理"><img src="/img/80857290_p0.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="21-内存与内存管理"/></a><div class="content"><a class="title" href="/2021/12/19/21-%E5%86%85%E5%AD%98%E4%B8%8E%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" title="21-内存与内存管理">21-内存与内存管理</a><time datetime="2021-12-19T09:57:53.000Z" title="Created 2021-12-19 17:57:53">2021-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/11/20-%E6%AD%BB%E9%94%81/" title="20-死锁"><img src="/img/81934649_p0.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="20-死锁"/></a><div class="content"><a class="title" href="/2021/12/11/20-%E6%AD%BB%E9%94%81/" title="20-死锁">20-死锁</a><time datetime="2021-12-11T06:20:20.000Z" title="Created 2021-12-11 14:20:20">2021-12-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/28/19-%E7%AE%A1%E7%A8%8B/" title="19-管程"><img src="/img/72581222_p0.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="19-管程"/></a><div class="content"><a class="title" href="/2021/11/28/19-%E7%AE%A1%E7%A8%8B/" title="19-管程">19-管程</a><time datetime="2021-11-28T08:39:08.000Z" title="Created 2021-11-28 16:39:08">2021-11-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/17/18-%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%EF%BC%88%E5%90%B8%E7%83%9F%E8%80%85%EF%BC%8C%E8%AF%BB%E8%80%85-%E5%86%99%E8%80%85%E7%AD%89%EF%BC%89/" title="18-信号量相关问题（吸烟者，读者-写者等）"><img src="/img/73604608_p0.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="18-信号量相关问题（吸烟者，读者-写者等）"/></a><div class="content"><a class="title" href="/2021/11/17/18-%E4%BF%A1%E5%8F%B7%E9%87%8F%E7%9B%B8%E5%85%B3%E9%97%AE%E9%A2%98%EF%BC%88%E5%90%B8%E7%83%9F%E8%80%85%EF%BC%8C%E8%AF%BB%E8%80%85-%E5%86%99%E8%80%85%E7%AD%89%EF%BC%89/" title="18-信号量相关问题（吸烟者，读者-写者等）">18-信号量相关问题（吸烟者，读者-写者等）</a><time datetime="2021-11-17T12:08:38.000Z" title="Created 2021-11-17 20:08:38">2021-11-17</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/img/77593329_p0.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Ywrby</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">Local search</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css"><script src="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex-copytex@latest/dist/katex-copytex.min.css"><script>(() => {
  document.querySelectorAll('#article-container span.katex-display').forEach(item => {
    btf.wrap(item, 'div', { class: 'katex-wrap'})
  })
})()</script><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'QkExI1n70lz2qs7pVpmFeKqp-gzGzoHsz',
      appKey: 'WhG61iDj96bflhu6CIpmE9V8',
      placeholder: 'Please leave your footprints',
      avatar: 'mp',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: false,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="5402995011" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-lrctype=0	data-order="random" data-preload="none" data-autoplay="true" muted></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>